package cn.iocoder.yudao.module.scrm.controller.admin.marketing;

import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.scrm.controller.admin.marketing.vo.content.*;
import cn.iocoder.yudao.module.scrm.dal.dataobject.marketing.MarketingContentDO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.order.OrderDO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.receivable.ReceivableDO;
import cn.iocoder.yudao.module.scrm.service.marketing.content.MarketingContentService;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.security.PermitAll;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import java.util.stream.Stream;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertListByFlatMap;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.common.util.collection.MapUtils.findAndThen;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 营销内容")
@RestController
@RequestMapping("/scrm/marketing-content")
@Validated
public class MarketingContentController {

    @Resource
    private MarketingContentService marketingContentService;

    @PostMapping("/create")
    @Operation(summary = "创建营销内容")
    @PreAuthorize("@ss.hasPermission('scrm:marketing-content:create')")
    @OperateLog(enable = false) // 不记录操作日志，因为不需要
    public CommonResult<Integer> createMarketingContent(@Valid @RequestBody MarketingContentSaveReqVO createReqVO ) {
        return success(marketingContentService.createMarketingContent(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新营销内容")
    @PreAuthorize("@ss.hasPermission('scrm:marketing-content:update')")
    @OperateLog(enable = false) // 不记录操作日志，因为不需要
    public CommonResult<Boolean> updateMarketingContent(@Valid @RequestBody MarketingContentSaveReqVO updateReqVO) {
        marketingContentService.updateMarketingContent(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除营销内容")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('scrm:marketing-content:delete')")
    public CommonResult<Boolean> deleteMarketingContent(@RequestParam("id") Integer id) {
        marketingContentService.deleteMarketingContent(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得营销内容")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PermitAll
    @TenantIgnore
    public CommonResult<MarketingContentRespByTypeVO> getMarketingContent(@RequestParam("id") Integer id) {
        return success(marketingContentService.getMarketingContent(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得营销内容分页")
    @PreAuthorize("@ss.hasPermission('scrm:marketing-content:query')")
    public CommonResult<PageResult<MarketingContentRespVO>> getMarketingContentPage(@Valid MarketingContentPageReqVO pageReqVO) {
        PageResult<MarketingContentDO> pageResult = marketingContentService.getMarketingContentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MarketingContentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出营销内容 Excel")
    @PreAuthorize("@ss.hasPermission('scrm:marketing-content:export')")
    @OperateLog(type = EXPORT)
    public void exportMarketingContentExcel(@Valid MarketingContentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MarketingContentDO> list = marketingContentService.getMarketingContentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "营销内容.xls", "数据", MarketingContentRespVO.class,
                        BeanUtils.toBean(list, MarketingContentRespVO.class));
    }

    @GetMapping("/pageByType")
    @Operation(summary = "获得营销内容分页-更具字典类型和部门id和分类进行查询入参vo")
    @PreAuthorize("@ss.hasPermission('scrm:marketing-content:query')")
    public CommonResult<PageResult<MarketingContentRespByTypeVO>> getMarketingContentPage(@Valid MarketingContentPageReqByTypeVO pageReqVO) {
        PageResult<MarketingContentRespByTypeVO> pageResult = marketingContentService.getMarketingContentByTypePage(pageReqVO);
        return success(pageResult);
    }


    @GetMapping("/page/notCheckDataPermissionTenant")
    @Operation(summary = "获得营销内容分页-不校验权限和租户")
    @PermitAll
    @TenantIgnore
    public CommonResult<PageResult<MarketingContentRespVO>> notCheckDataPermissionTenant(@Valid MarketingContentPageReqVO pageReqVO) {
        PageResult<MarketingContentDO> pageResult = marketingContentService.notCheckDataPermissionTenant(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MarketingContentRespVO.class));
    }

    @GetMapping("/get/notCheckDataPermissionTenant")
    @Operation(summary = "获得营销内容-查看详情不不校验租户与数据权限")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PermitAll
    @TenantIgnore
    public CommonResult<MarketingContentRespByTypeVO> getNotCheckDataPermissionTenant(@RequestParam("id") Integer id) {
        return success(marketingContentService.getMarketingContent(id));
    }



}